//
//  MonthTitleView.m
//  carcareIOS
//
//  Created by ileo on 16/7/7.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MonthTitleView.h"

@interface MonthTitleView ()

@property (nonatomic, strong) UILabel *monthTitleLabel;
@property (nonatomic, strong) UIButton *lastMonthButton;
@property (nonatomic, strong) UIButton *nextMonthButton;
@property (nonatomic, strong) CAShapeLayer *lastMonth;
@property (nonatomic, strong) CAShapeLayer *nextMonth;

@end

@implementation MonthTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.monthTitleLabel];
        [self.layer addSublayer:self.lastMonth];
        [self.layer addSublayer:self.nextMonth];
        [self addSubview:self.lastMonthButton];
        [self addSubview:self.nextMonthButton];
        self.lastMonth.position = CGPointMake(self.lastMonthButton.centerX, self.lastMonthButton.centerY - 3);
        self.nextMonth.position = CGPointMake(self.nextMonthButton.centerX, self.nextMonthButton.centerY - 3);

    }
    return self;
}

-(void)updateTitle:(NSString *)title canLoadLast:(BOOL)canLoadLast canLoadNext:(BOOL)canLoadNext{
    self.monthTitleLabel.text = title;
    self.lastMonth.fillColor = canLoadLast ? COLOR_HEXSTRING(@"#626262").CGColor : COLOR_HEXSTRING(@"#d4d4d4").CGColor;
    self.nextMonth.fillColor = canLoadNext ? COLOR_HEXSTRING(@"#626262").CGColor : COLOR_HEXSTRING(@"#d4d4d4").CGColor;
    self.lastMonthButton.enabled = canLoadLast;
    self.nextMonthButton.enabled = canLoadNext;
}


#pragma mark - set get

-(UILabel *)monthTitleLabel{
    if (!_monthTitleLabel) {
        _monthTitleLabel = [[UILabel labelWithFrame:CGRectMake(0, 0, self.width, self.height) font:FONT_DEFAULT_Light(15) text:nil textColor:COLOR_HEXSTRING(@"#424242")] labelResetTextAlignment:NSTextAlignmentCenter];
    }
    return _monthTitleLabel;
}

-(CAShapeLayer *)lastMonth{
    if (!_lastMonth) {
        _lastMonth = [CAShapeLayer layer];
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 0, 5);
        CGPathAddLineToPoint(path, NULL, 7, 0);
        CGPathAddLineToPoint(path, NULL, 7, 10);
        CGPathAddLineToPoint(path, NULL, 0, 5);
        _lastMonth.path = path;
        CGPathRelease(path);
    }
    return _lastMonth;
}

-(CAShapeLayer *)nextMonth{
    if (!_nextMonth) {
        _nextMonth = [CAShapeLayer layer];
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 7, 5);
        CGPathAddLineToPoint(path, NULL, 0, 10);
        CGPathAddLineToPoint(path, NULL, 0, 0);
        CGPathAddLineToPoint(path, NULL, 7, 5);
        _nextMonth.path = path;
        CGPathRelease(path);
    }
    return _nextMonth;
}

-(UIButton *)lastMonthButton{
    if (!_lastMonthButton) {
        __weak __typeof(self) wself = self;
        _lastMonthButton = [UIButton buttonEmptyWithFrame:CGRectMake(0, 0, 60, self.height) click:^{
            if (wself.delegate) {
                [wself.delegate monthTitleViewClickOnLast];
            }
        }];
    }
    return _lastMonthButton;
}

-(UIButton *)nextMonthButton{
    if (!_nextMonthButton) {
        __weak __typeof(self) wself = self;
        _nextMonthButton = [UIButton buttonEmptyWithFrame:CGRectMake(self.width - 60, 0, 60, self.height) click:^{
            if (wself.delegate) {
                [wself.delegate monthTitleViewClickOnNext];
            }
        }];
    }
    return _nextMonthButton;
}


@end
